package mq;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import billing.inf.hessian.IHelloWorld;

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = { "/spring.base.xml", "/spring.hessian.xml"})
public class ReadMq {
	
	@Autowired
	private IHelloWorld iHelloWorld;
	final String key = "1";
	final String session = "1";

	@Test
	public void testGetMsg() throws Exception {
		
		final Integer msgSize = 2;
		while(true){
			List<String> msgList = null;
			do{
				try{
					msgList = iHelloWorld.getMessage(key, session,msgSize);
				}catch (Exception e) {
				}
			
				if(null != msgList){
					for (String msg : msgList) {
						
						doProccess(msg);
						iHelloWorld.doSub(msg);
					}
				}
			}while(null != msgList && msgSize == msgList.size());
			Thread.sleep(5000);
		}
	}

	private void doProccess(String msg) {
		System.out.println(SimpleDateFormat.getInstance().format(new Date()) + "message[ "+msg+" ]");
	}
}
